package PTA_JAVA.JavaA.Javademo19;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<Person> personList = new ArrayList<>();
        List<Student> stuList = new ArrayList<>();
        List<Employee> empList = new ArrayList<>();
        while (true) {
            String[] n=sc.nextLine().split(" ");
            if(n[0].equals("s")){
                Student s = new Student(n[1],Integer.parseInt(n[2]),Boolean.parseBoolean(n[3]),n[4], n[5]);
                personList.add(s);
            }else if(n[0].equals("e")){
                Employee e = new Employee(n[1], Integer.parseInt(n[2]),Boolean.parseBoolean(n[3]),Double.parseDouble(n[4]),new Company(n[5]));
                personList.add(e);
            }else{
                break;
            }
        }
        Collections.sort(personList, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                if(o1.name.equals(null) && o2.name.equals(null)){
                    return 0;
                }
                if(o1.name.compareTo(o2.name)==0){
                    return o1.age-o2.age;
                }else {
                    return o1.name.compareTo(o2.name);
                }
            }
        });
        for(Person i:personList){
            System.out.println(i.toString());
        }
        if(sc.next().equals("exit")) {
            sc.close();
            return;
        }
        for(Person i:personList){
            if(i instanceof Student){
                int f=0;
                for(Student j:stuList){
                    if(j.equals(i)){
                        f=1;
                        break;
                    }
                }
                if(f==0){
                    stuList.add((Student) i);
                }
            }else if(i instanceof Employee){
                int f=0;
                for(Employee j:empList){
                    if(j.equals(i)){
                        f=1;
                        break;
                    }
                }
                if(f==0){
                    empList.add((Employee) i);
                }
            }
        }
        System.out.println("stuList");
        for (Student i : stuList) {
            System.out.println(i);
        }
        System.out.println("empList");
        for (Employee i : empList) {
            System.out.println(i);
        }
    }
}
abstract class Person{
    public String name;
    public int age;
    private boolean gender;

    public Person(String name, int age, boolean gender){
        this.name=name;
        this.age=age;
        this.gender=gender;
    }

    @Override
    public String toString(){
        return this.name+"-"+this.age+"-"+this.gender;
    }       //返回"name-age-gender"格式的字符串
    @Override
    public boolean equals(Object obj){
        Person o = (Person) obj;
        if(this.name.compareTo(o.name)==0){
            if(this.age-o.age==0){
                if(this.gender==o.gender){
                    return true;
                }
            }
        }
        return false;
    }//比较name、age、gender,都相同返回true,否则返回false

}
class Student extends Person{
    private String stuNo;
    private String clazz;
    //建议使用super复用Person类的相关有参构造函数
    public Student(String name, int age, boolean gender, String stuNo, String clazz){
        super(name,age,gender);
        this.stuNo = stuNo;
        this.clazz = clazz;
    }
    @Override
    public String toString(){
        return "Student:"+super.toString()+"-"+this.stuNo+"-"+this.clazz;
    }        //返回 “Student:person的toString-stuNo-clazz”格式的字符串
    @Override
    public boolean equals(Object obj){
        if(super.equals(obj)){
            Student o = (Student) obj;
            return (this.stuNo.compareTo(o.stuNo)==0) && (this.clazz.compareTo(o.clazz)==0);
        }else{
            return false;
        }
    }//首先调用父类的equals方法,如果返回true，则继续比较stuNo与clazz。
}
class Company{
    public String name;
    public Company(String name){
        this.name=name;
    }
    @Override
    public String toString(){
        return this.name;
    }       //直接返回name
    @Override
    public boolean equals(Object obj){
        return obj.equals(this.name);
    }//name相同返回true

}
class Employee extends Person{
    public Company company;
    private double salary;

    //建议使用super复用Person类的相关有参构造函数
    public Employee(String name, int age, boolean gender, double salary, Company company){
        super(name,age,gender);
        this.company = company;
        this.salary=salary;
    }
    @Override
    public String toString(){
        return "Employee:"+super.toString()+"-"+this.company.toString()+"-"+this.salary;
    }         //返回"Employee:person的toString-company-salary"格式的字符串
    @Override
    public boolean equals(Object obj){
        if(super.equals(obj)){
            Employee o=(Employee) obj;
            DecimalFormat f = new DecimalFormat("#.#");
            return this.company.equals(o.company) && f.format(this.salary).equals(f.format(o.salary));
        }else{
            return false;
        }
    }//首先调用父类的equals方法,如果返回true。再比较company与salary。
//比较salary属性时，使用DecimalFormat df = new DecimalFormat("#.#");保留1位小数

}
